package com.jia.interviewPro;

/**
 * @program: Leetcode
 * @description:  https://leetcode-cn.com/problems/color-fill-lcci/
 * @author: STU756
 * @create: 2020-09-03 11:13
 */
public class Pro0810 {
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        if(image[sr][sc] == newColor) return image;
        dfs(image, sr, sc, newColor, image[sr][sc]);
        return image;
    }
    int[] dx = {1, -1, 0, 0};
    int[] dy = {0, 0, -1, 1};
    private void dfs(int[][] image, int sr, int sc, int newColor, int oldColor) {
        if(sr < 0 || sr >= image.length || sc < 0 || sc >= image[0].length || image[sr][sc] == newColor || image[sr][sc]!=oldColor) return;
        image[sr][sc] = newColor;
        for(int i = 0; i < 4; i++) {
            int nx = sr + dx[i];
            int ny = sc + dy[i];
            dfs(image, nx, ny, newColor, oldColor);
        }
    }
}
